﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Core.Contracts.NewHouse;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Model.DbModels.Common;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.Security;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Areas.Organization.Controllers
{
    [Description("跟进信息")]
    public class MessageBoardsController : BaseController
    {
        public IMessageBoardsContract MessageBoardsContract { get; set; }
        public IStaffContract StaffContract { get; set; }
        public IIntermediaryContract IntermediaryContract { get; set; }
        public IBuildingsContract BuildingsContract { get; set; }
        #region 列表
        [Description("跟进信息-列表")]
        public ActionResult Index(int InterfixID, int MessageBoardsType)
        {
            ViewBag.InterfixID = InterfixID;
            ViewBag.MessageBoardsType = MessageBoardsType;
            return View();
        }
        public ActionResult IndexAsync(int pageIndex, int pageSize, int InterfixID, int MessageBoardsType, string search = "")
        {
            var query = MessageBoardsContract.Entities.Where(m => !m.IsDeleted);
            if (search != "")
            {
                query = query.Where(m => m.Content.Contains(search));
            }
            if (MessageBoardsType != 0)
            {
                query = query.Where(m => m.MessageBoardsType == MessageBoardsType);
            }
            if (InterfixID != 0)
            {
                query = query.Where(m => m.InterfixID == InterfixID);
            }
            var list = from q in query
                        join s in StaffContract.Entities.Where(m => !m.IsDeleted)
                        on q.StaffID equals s.Id 
                        select new
                        {
                            Id = q.Id,
                            MessageBoardsType = q.MessageBoardsType,
                            StaffID = s.Name,
                            //InterfixID = MessageBoardsType == 1 ? "i.Name" : "b.Name",
                            Content = q.Content,
                            CreatedTime = q.CreatedTime
                        };
            //var a = list.ToList();
            var total = list.Count();
            //默认给一个排序的字段
            list = list.OrderByDescending(m => m.Id);

            //分页(假如total == 0，则取消分页查询，提高性能)
            list = total > 0 ? list.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : list;

            //此处可以采用匿名对象 GridData<object> 
            GridData<object> gridData = new GridData<object>() { Total = total, Rows = list.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }
        #endregion
        #region 添加
        [Description("跟进信息-楼盘添加")]
        public ActionResult Insert(int InterfixID,int MessageBoardsType)
        {
            ViewBag.InterfixID = InterfixID;
            ViewBag.MessageBoardsType = MessageBoardsType;
            return View();
        }
        [HttpPost]
        public ActionResult InsertAsync(MessageBoards entity)
        {
            //所有AJAX的结果，返回统一数据格式
            var result = new AjaxResult();
            var cookies = Common.CookiesManagement.GetCookies();
            var ticket = Common.CookiesManagement.GetTicket(cookies);
            var model = Common.CookiesManagement.GetLoginModel(ticket);
            entity.StaffID = model.Id; 


            try
            {
                var count = MessageBoardsContract.Insert(entity);
                if (count > 0)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "录入成功";
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "录入失败";
                }
            }
            catch (Exception e)
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion
        #region 修改
        [Description("跟进信息-修改")]
        public ActionResult Edit(int id)
        {
            var entity = MessageBoardsContract.GetByKey(id);
            //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
            if (entity == null) throw new Exception("未找到该实体");
            ViewBag.entity = entity;
            return View();
        }
        [HttpPost]
        public ActionResult EditListAsync(MessageBoards entity)
        {
            var result = new AjaxResult();
            try
            {
                var cookies = Common.CookiesManagement.GetCookies();
                var ticket = Common.CookiesManagement.GetTicket(cookies);
                var model = Common.CookiesManagement.GetLoginModel(ticket);
                entity.StaffID = model.Id; 




                var count = MessageBoardsContract.Update(entity);
                if (count > 0)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "修改成功";
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "修改失败";
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion
        #region 删除
        [Description("跟进信息-删除")]
        [HttpPost]
        public ActionResult DeleteListAsync(string ids)
        {
            var result = new AjaxResult();
            try
            {
                string[] idsStr = ids.Split(new char[] { ',' });
                int[] idsInt = Array.ConvertAll(idsStr, id => Convert.ToInt32(id));
                MessageBoardsContract.UpdateDirect(m => idsInt.Contains(m.Id), m => new MessageBoards { IsDeleted = true });

                result.Type = AjaxResultType.Success;
                result.Content = "删除成功";
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion 
    }
}